package com.example.dell.jakatra.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.example.dell.jakatra.R;
import com.example.dell.jakatra.activity.AbsBaseActivity;
import com.example.dell.jakatra.api.RegisterImpl;
import com.example.dell.jakatra.login.GoogleLoginManager;
import com.example.dell.jakatra.retrofit.BaseAbsResponse;
import com.example.dell.jakatra.retrofit.RetrofitManager;
import com.example.dell.jakatra.rxjava.BaseSubscriber;
import com.example.dell.jakatra.rxjava.RxSchedulersHelper;
import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;

import rx.Subscriber;

public class MainActivity extends AbsBaseActivity {

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }


    @Override
    public void initViewGroupConfig(boolean isUseMultipleView, boolean isShowLoad) {
        super.initViewGroupConfig(true, false);
    }

    private CallbackManager callbackManager;

    @Override
    public void initViews(Bundle savedInstanceState) {
        GoogleLoginManager.getInstance().initLogin(MainActivity.this);
        findViewById(R.id.sign_in_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                GoogleLoginManager.getInstance().login(MainActivity.this);
            }
        });


        callbackManager = CallbackManager.Factory.create();

        final LoginButton loginButton = findViewById(R.id.login_button);
        loginButton.setReadPermissions("email");
        // If using in a fragment
//        loginButton.setFragment(this);

        // Callback registration
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                // App code
                Log.d("loginResult", "onSuccess");
                AccessToken accessToken = loginResult.getAccessToken();
            }

            @Override
            public void onCancel() {
                // App code
                Log.d("loginResult", "onCancel");

            }


            @Override
            public void onError(FacebookException exception) {
                // App code.
                Log.d("loginResult", "onError");

            }
        });


        RetrofitManager
                .getInstance()
                .createApi(RegisterImpl.class)
                .sendSms("15925641757")
                .compose(RxSchedulersHelper.observeOnMainThread())
                .subscribe(new BaseSubscriber<BaseAbsResponse>() {
                    @Override
                    public void onFinish() {

                    }

                    @Override
                    public void onErr(Throwable e) {

                    }

                    @Override
                    public void onSuccess(BaseAbsResponse baseAbsResponse) {

                    }
                });

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
        if (requestCode == GoogleLoginManager.RC_SIGN_IN) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            Log.i("robin", "handleSignInResult:" + result.isSuccess());
            if (result.isSuccess()) {
                Log.i("robin", "成功");
                GoogleSignInAccount acct = result.getSignInAccount();
                if (acct != null) {
                    Log.i("robin", "用户名是:" + acct.getDisplayName());
                    Log.i("robin", "用户email是:" + acct.getEmail());
                    Log.i("robin", "用户头像是:" + acct.getPhotoUrl());
                    Log.i("robin", "用户Id是:" + acct.getId());//之后就可以更新UI了
                    Log.i("robin", "用户IdToken是:" + acct.getIdToken());
                }
            } else {
                Log.i("robin", "没有成功" + result.getStatus());
            }
        }
    }


}
